package com.mouse.controller.admin;//package com.mouse.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mouse.common.lang.Result;
import com.mouse.entity.GeneAllele;
import com.mouse.entity.GeneCategory;
import com.mouse.mapper.GeneCategoryMapper;
import com.mouse.service.GeneCategoryService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiresAuthentication
@RequestMapping("/admin/gene-category")
public class AdminGeneCategoryController {

    @Autowired
    GeneCategoryService geneCategoryService;


    /***
     * 输入主键不为null的请求，代表更新
     * 输入主键为null的请求，代表新增
     * @param geneCategory
     * @return
     */
    @PostMapping("/createOrUpdate/")
    public Result edit(@Validated @RequestBody GeneCategory geneCategory) {

//        Assert.isTrue(false, "公开版不能任意编辑！");
        String prefix;
        GeneCategory temp = new GeneCategory();
        if (geneCategory.getGid() != null) {
            temp = geneCategoryService.getById(geneCategory.getGid());
            prefix = "更新";
        } else {

            temp = new GeneCategory();
            prefix = "插入";
        }
//        temp.setCreated(LocalDateTime.now());

        BeanUtil.copyProperties(geneCategory, temp);
        geneCategoryService.saveOrUpdate(temp);

        return Result.succ(prefix + "成功");
    }


    @PostMapping("/delete/{id}")
    public Result delete(@Validated @PathVariable(name = "id") Long id) {
        GeneCategory geneCategory = geneCategoryService.getById(id);
        Assert.notNull(geneCategory, "This info doesn't exist");
        geneCategoryService.removeById(id);
        return Result.succ("Deleted");
    }
}
